package com.gm.commonlib.views.choiceview;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.gm.commonlib.R;

import java.util.ArrayList;
import java.util.List;

public class MyCustomItemChoosenView extends LinearLayout {

    private GridView mGVCityChoosenGridview;
    private GridViewAdapter mGridViewAdapter;
    private TextView mTitle;
    private View mChoosenView;
    private int maxRow = -1;

    public MyCustomItemChoosenView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        initView();
    }

    public MyCustomItemChoosenView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyCustomItemChoosenView(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        // 造成错误的代码段
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setOrientation(LinearLayout.VERTICAL);
        inflater.inflate(R.layout.custom_item_choosen_view, this, true);
        mGVCityChoosenGridview = (GridView) findViewById(R.id.gv_item_choosen_gridview);
        mTitle = (TextView) findViewById(R.id.tv_item_choosen_title);
        mGridViewAdapter = new GridViewAdapter(getContext());
        mGVCityChoosenGridview.setAdapter(mGridViewAdapter);
    }

    private static class GridViewAdapter extends BaseAdapter {
        List<CustomItemChoosenEntity> dataList;
        LayoutInflater layoutInflater;

        public GridViewAdapter(Context context) {
            dataList = new ArrayList<CustomItemChoosenEntity>();
            layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void setDataList(List<? extends CustomItemChoosenEntity> dataList) {
            this.dataList.clear();
            if (dataList != null) {
                this.dataList.addAll(dataList);
            }
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public CustomItemChoosenEntity getItem(int position) {
            if (position < 0 || position >= getCount()) {
                return null;
            }
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textview;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.custom_grid_item, parent, false);
                textview = (TextView) convertView.findViewById(R.id.tv_title);
                convertView.setTag(textview);
            } else {
                textview = (TextView) convertView.getTag();
            }
            CustomItemChoosenEntity choosenEntity = getItem(position);
            if (choosenEntity != null) {
                textview.setText(choosenEntity.getText());
            }
            return convertView;
        }
    }

    public int getMaxRow() {
        return maxRow;
    }

    /**
     * 设置最多显示行数，超过这个行数则需要滑动才能看到
     *
     * @param maxRow 最大行数
     */
    public void setMaxRow(int maxRow) {
        this.maxRow = maxRow;
    }

    public void setGridViewMaxHeight(int maxHeight) {
        ViewGroup.LayoutParams params = mGVCityChoosenGridview.getLayoutParams();
        int oldHeight = params.height;
        if (oldHeight > maxHeight) {
            params.height = maxHeight;
        }
        mGVCityChoosenGridview.setLayoutParams(params);
        mGVCityChoosenGridview.invalidate();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mGVCityChoosenGridview.setOnItemClickListener(listener);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        mGVCityChoosenGridview.setOnItemLongClickListener(listener);
    }

    /**
     * 设置显示内容
     *
     * @param dataList 内容list
     */
    public void setDataList(List<? extends CustomItemChoosenEntity> dataList) {
        if (dataList != null) {
            mGridViewAdapter.setDataList(dataList);

        }
    }

    /**
     * 设置标题
     *
     * @param title 标题
     */
    public void setTitle(String title) {
        if (title == null) {
            title = "";
        }
        mTitle.setText(title);
    }

    /**
     * 设置一行显示个数
     *
     * @param Num 个数
     */
    public void setNumColumns(int Num) {
        mGVCityChoosenGridview.setNumColumns(Num);
        //ViewUtil.setGridViewHeightBasedOnChildren(mGVCityChoosenGridview, Num, maxRow);

    }

    public void hiddenTitle() {
        if (mTitle != null) {
            mTitle.setVisibility(View.GONE);
        }

    }

}
